package com.kwan.widget.float_view.floatwindow;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;

import org.jetbrains.annotations.NotNull;

import timber.log.Timber;

/**
 * Created by yhao on 17-12-1.
 * 用于控制悬浮窗显示周期
 * 使用了三种方法针对返回桌面时隐藏悬浮按钮
 * 1.startCount计数，针对back到桌面可以及时隐藏
 * 2.监听home键，从而及时隐藏
 * 3.resumeCount计时，针对一些只执行onPause不执行onStop的奇葩情况
 */

public class FloatLifecycle extends BroadcastReceiver
		implements Application.ActivityLifecycleCallbacks {

	private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
	private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
	private static final long delay = 300;
	private Handler mHandler;
	private final Class<? extends Activity>[] activities;
	private boolean showFlag;
	private int startCount;
	private int resumeCount;
	private boolean appBackground;
	private LifecycleListener mLifecycleListener;
	private static ResumedListener sResumedListener;
	private static int num = 0;


	public FloatLifecycle(Context applicationContext,
						  boolean showFlag,
						  Class<? extends Activity>[] activities,
						  LifecycleListener lifecycleListener) {

		this.showFlag = showFlag;
		this.activities = activities;
		num++;
		mLifecycleListener = lifecycleListener;
		mHandler = new Handler();
		((Application) applicationContext).registerActivityLifecycleCallbacks(this);
		applicationContext.registerReceiver(this, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
	}

	public static void setResumedListener(ResumedListener resumedListener) {
		sResumedListener = resumedListener;
	}

	private boolean needShow(Activity activity) {
		if (activities == null) {
			return true;
		}
		for (Class<? extends Activity> a : activities) {
			if (a.isInstance(activity)) {
				return showFlag;
			}
		}
		return !showFlag;
	}


	@Override
	public void onActivityResumed(@NotNull Activity activity) {
		if (sResumedListener != null) {
			num--;
			if (num == 0) {
				sResumedListener.onResumed();
				sResumedListener = null;
			}
		}
		resumeCount++;
		if (needShow(activity)) {
			Timber.e("onActivityResumed show:" + activity);
			mLifecycleListener.onResumedShow(activity);
		} else {
			Timber.e("onActivityResumed hide:" + activity);
			mLifecycleListener.onResumedHide(activity);
		}

		if (appBackground) {
			appBackground = false;
		}
	}

	@Override
	public void onActivityPaused(final @NotNull Activity activity) {
		resumeCount--;
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				if (resumeCount == 0) {
					appBackground = true;
					mLifecycleListener.onBackToDesktop(activity);
				}
			}
		}, delay);

	}

	@Override
	public void onActivityStarted(@NotNull Activity activity) {
		startCount++;
	}


	@Override
	public void onActivityStopped(@NotNull Activity activity) {
		startCount--;
		if (startCount == 0) {
			mLifecycleListener.onBackToDesktop(activity);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if (action != null && action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
			String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
			if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
				//mLifecycleListener.onBackToDesktop();
			}
		}
	}


	@Override
	public void onActivityCreated(@NotNull Activity activity, Bundle savedInstanceState) {
		mLifecycleListener.onCreate(activity);
	}


	@Override
	public void onActivitySaveInstanceState(@NotNull Activity activity, @NotNull Bundle outState) {

	}

	@Override
	public void onActivityDestroyed(@NotNull Activity activity) {
		mLifecycleListener.onDestroyed(activity);
	}


}
